package com.lc.railway.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.railway.R;

import org.json.JSONArray;
import org.json.JSONObject;

import androidx.recyclerview.widget.RecyclerView;


/**
 * @Title：
 * @Description：
 * @Author：huqinghan(19044311)
 * @Date：2021/3/24
 */
public class TestColumnnewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    JSONArray jsa;
    Context mContext;
    CallBack mCallBack;
    int mCurrentIndex = 0;
    public TestColumnnewAdapter(Context context, CallBack callBack){
        this.mContext = context;
        this.mCallBack = callBack;
    }

    public void setCurrentIndex(int mCurrentIndex){
        this.mCurrentIndex = mCurrentIndex;
        notifyDataSetChanged();
    }

    public void setData(JSONArray jsa){
        this.jsa = jsa;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_test_columnnew_group, viewGroup, false);
        ItemHolder holder = new ItemHolder(view);
        holder.titile = view.findViewById(R.id.header_title);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof ItemHolder) {
            ItemHolder holder = (ItemHolder) viewHolder;
            final int index = position;
            JSONObject jo = getItem(position);
            holder.titile.setText(jo.optString("classifyName"));
            holder.titile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mCallBack != null){
                        mCallBack.onClick(index + 1);
//                        mCurrentIndex = index;
//                        notifyDataSetChanged();
                    }
                }
            });
            holder.titile.setBackgroundColor(mCurrentIndex == position ? 0xFFFF00FF : 0xFFFFFF00);
        }
    }

    @Override
    public int getItemCount() {
        return jsa != null ? jsa.length() : 0;
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        TextView titile;

        public ItemHolder(View itemView) {
            super(itemView);
        }
    }

    public JSONObject getItem(int position) {
        JSONObject jo = jsa.optJSONObject(position);
        return jo;
    }

    public interface CallBack{
        void onClick(int index);
    }
}